﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace PolarSonicLink
{
    public partial class SimpleGraph : UserControl
    {
        public SimpleGraph()
        {
            InitializeComponent(); 
        }

        public void Redraw(Graphics g)
        {
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            g.FillRectangle(Brushes.Ivory, DisplayRectangle);

            // X&Y Axis
            g.DrawLine(Pens.Maroon, new Point(0, DisplayRectangle.Bottom / 2), new Point(DisplayRectangle.Right, DisplayRectangle.Bottom / 2));

            if ( m_pointData != null )
            {
                int ZeroLineOffset = DisplayRectangle.Bottom / 2;
                float dataSize = (float) m_pointData.Length;
                PointF[] pointData = new PointF[m_pointData.Length];

                for ( int i = 0; i < m_pointData.Length; i++ )
                {
                    pointData[i].X = ( i / dataSize ) * DisplayRectangle.Width;
                    pointData[i].Y = (float) ( (double) ZeroLineOffset + ( m_pointData[i] * DisplayRectangle.Bottom ) );
                }

                g.DrawLines(Pens.DarkSlateGray, pointData);
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Redraw(e.Graphics);
        }

        protected override void OnResize(EventArgs e)
        {
            Invalidate();
        }

        public void UpdateData(double[] data)
        {
            m_pointData = new double[data.Length];
            data.CopyTo(m_pointData, 0);

            Invalidate();
        }

        private double []m_pointData;

        
    }
}
